/**
 * 
 */
package nl.sportan.samenloop.timers;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import nl.sportan.samenloop.R;
import nl.sportan.samenloop.communication.ClockReader;
import nl.sportan.samenloop.communication.Result;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Bram Wiekens
 * 
 */
public class CountDownPanel extends LinearLayout {

	private Timer timer = new Timer();
	private long start;
	private boolean started = false;
	private Handler handler;

	private static long DAY = 24 * 3600000;
	private static long HOUR = 3600000;
	private static long MINUTE = 60000;
	private static long SECOND = 1000;

	private TextView clockText;
	private Context context;
	private View layoutView;

	public CountDownPanel(Context context, Date startDate) {
		super(context);
		this.context = context;
		handler = new Handler();
		
		start = startDate.getTime();

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		layoutView = inflater.inflate(R.layout.countdown_layout, null);

		clockText = (TextView) layoutView.findViewById(R.id.CountdownClockText);

		addView(layoutView);

	}
	
	private class UpdateStyle implements Runnable {

		public void run() {
			clockText.setTypeface(clockText.getTypeface(), Typeface.BOLD);
			clockText.setTextColor(R.color.purple);
		}
		
	}
	
	
	private class ClockResult implements Result<Long> {

		public void onSucces(Long result) {
			if (result != null && result > 0) {
				start = result + DAY;
				started = true;

				handler.post(new UpdateStyle());
			}
		
			timer = new Timer();

			TimerTask ttask = new ClockTimer();

			timer.scheduleAtFixedRate(ttask, 0, 1000);
			
		}

		public void onFailure(Throwable e) {
			// TODO Auto-generated method stub
			
		}
		
	}

	@Override
	protected void onAttachedToWindow() {
		ClockReader clockReader = new ClockReader(context.getString(R.string.clock_url));
		clockReader.setResultHandler(new ClockResult());
		
		Thread t = new Thread(clockReader);
		t.start();
		
		super.onAttachedToWindow();

	}

	protected void onDetachedFromWindow() {
		timer.cancel();
	}

	private String prepend(long num) {
		return num < 10 ? "0" + num : "" + num;
	}

	private void setClock(long days, long hours, long minutes, long seconds) {
		String dayStr = days > 0 ? days +  " dagen " : "" ;
		String time = "";
		if (!started) {
		time = dayStr + prepend(hours) + ":"
				+ prepend(minutes) + ":" + prepend(seconds);
		} else {
			time = dayStr + prepend(hours) + ":"
					+ prepend(minutes) + ":" + prepend(seconds) + " SamenLopen!";
		}
		post(new ClockUpdater(time));

	}

	private void updateClock() {
		long now = Calendar.getInstance().getTimeInMillis();

		long diff = start - now > 0 ? start - now : 0;

		long days = diff / DAY;

		diff = diff - (days * DAY);

		long hours = diff / HOUR;

		diff = diff - (hours * HOUR);

		long minutes = diff / MINUTE;

		diff = diff - (minutes * MINUTE);

		long seconds = diff / SECOND;

		setClock(days, hours, minutes, seconds);
	}

	private class ClockUpdater implements Runnable {

		String time;

		public ClockUpdater(String time) {
			this.time = time;
		}

		public void run() {

			clockText.setText(time);

		}

	}

	private class ClockTimer extends TimerTask {

		@Override
		public void run() {

			updateClock();
		}

	}

}
